package com.lw.test;

import java.io.*;

/**
 * 序列化作用的案例
 * by lw on 2025年7月29日 10:32:36
 */
public class testMain {

    public static void saveSettings(UserSettings settings, String filename){
        try (ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(filename))) {
            oos.writeObject(settings);
            System.out.println("设置保存成功");
        } catch (IOException e) {
            System.err.println("保存设置失败: " + e.getMessage());
        }
    }

    // 从文件加载配置
    public static UserSettings loadSettings(String filename) {
        try (ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(filename))) {
            UserSettings settings = (UserSettings) ois.readObject();
            System.out.println("设置加载成功");
            return settings;
        } catch (IOException | ClassNotFoundException e) {
            System.err.println("加载设置失败: " + e.getMessage());
            return null;
        }
    }

    public static void main(String[] args) {
        UserSettings settings = new UserSettings("Dark", 14);
        settings.addFeatureFlag("AutoSave", true);
        settings.addFeatureFlag("Notifications", false);
        saveSettings(settings, "user.settings");
        // 加载设置
        UserSettings loadedSettings = loadSettings("user.settings");
        if (loadedSettings != null) {
            System.out.println("主题: " + loadedSettings.getTheme());
            System.out.println("字体大小: " + loadedSettings.getFontSize());
            System.out.println("功能标志: " + loadedSettings.getFeatureFlags());
        }
    }
}
